package com.xuecheng.content.controller;
import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.TeachplanMediaDTO;
import com.xuecheng.api.content.model.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.api.media.model.vo.BindTeachplanMediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;

import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import com.xuecheng.content.service.TeachplanService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 * 主分支切换
 *
 * @author itcast
 */
@RestController
public class CourseBaseController implements CourseBaseApi {
    @Autowired
    private CourseBaseService  courseBaseService;
    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private CoursePubService coursePubService;

    @PostMapping("course_pub/publish/{courseId}")
    public void publish(@PathVariable Long courseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        coursePubService.publish(courseId,companyId,false);
    }

    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,
                                  @RequestBody QueryCourseModel model) {
        Long companyId = UAASecurityUtil.getCompanyId();

        return courseBaseService.queryCourseList(params,model,companyId);
    }
    /**
     * 添加课程基本信息
     *
     * @param vo CourseBaseVO 封装的课程对象VO，接收前端数据
     * @return CourseBaseDTO 封装的课程对象DTO，返回给前端的数据
     */
    @Override
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        Long companyId = UAASecurityUtil.getCompanyId();
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);
        return  courseBaseService.createCourserBase(dto);
    }

    @Override
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {
        Long companyId = UAASecurityUtil.getCompanyId();
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);
        return courseBaseService.modifyCourseBase(dto);
    }

    @Override
    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        return courseBaseService.getCourseBaseById(courseBaseId,companyId);
    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.removeCourseBaseById(courseBaseId,companyId);
    }
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }
    /*
     * SpringMVC 中controller 多结果视图器并存
     * CourseBaseController ：
     * JackSonResolver （默认）--controller使用的是RestController
     * FreemarkerViewResolver
     * 1.添加依赖：spring-boot-starter-Freemarker
     * 2.添加配置：spring.freemarker.enabled = true
     * 3.方法的返回：controller的方法返回为ModelAndView对象
     * */
    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId,@PathVariable Long companyId) {
        Map<String, Object> dataMap = coursePubService.previewCourse(courseId, companyId);
        // 1.使用视图和数据模型对象
        ModelAndView modelAndView = new ModelAndView("learing_article",dataMap);
        return modelAndView;
    }



}
